<template>
<h1>员工列表综合练习</h1>
  <el-form style="width: 500px ;margin: 0 auto;" >
    <el-form-item label="姓名">
      <el-input v-model="user.name" placeholder="请输入员工姓名"></el-input>
    </el-form-item>
    <el-form-item label="工资">
      <el-input v-model="user.salary" placeholder="请输入员工工资"></el-input>
    </el-form-item>
    <el-form-item label="岗位">
      <el-input v-model="user.job" placeholder="请输入员工岗位"></el-input>
    </el-form-item>
  </el-form>
  <el-button type="primary" @click="add">提交</el-button>
  <el-table :data="arr" style="width: 100%" align="center">
    <el-table-column type="index" label="编号" width="200" align="center"></el-table-column>
    <el-table-column prop="name" label="姓名" width="200" align="center"></el-table-column>
    <el-table-column prop="salary" label="工资" width="200" align="center"></el-table-column>
    <el-table-column prop="job" label="岗位" width="200" align="center"></el-table-column>
    <el-table-column label="操作" width="200" align="center">
      <template #default="scope">
        <el-button type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";

const arr = ref([

]);
const user = ref({
  name:'',
  salary:'',
  job:''
});
const add = () => {
  if (user.value.name.trim() == ''){
    ElMessage.error('请输入员工姓名');
    return;
  }else if (user.value.salary.trim() == ''){
    ElMessage.error('请输入员工工资');
    return;
  }else if (user.value.job.trim() == ''){
    ElMessage.error('请输入员工工作岗位');
    return;
  }
  console.log(user.value);
  arr.value.push(user.value);
  user.value = {};

}
const del = (index,emp) => {
  if (confirm(`确定删除${emp.name}吗？`)){
    arr.value.splice(index,1);
    ElMessage.success('删除成功');
  }
}
</script>

<style scoped>

</style>